﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Laugh.Environment;
using Laugh;
using Laugh.Data.DatabaseCfg;
using LaughTest.Services;
using Laugh.Data;
using LaughTest.Domain;

namespace LaughTest
{
    /// <summary>
    /// UnitTest1 的摘要说明
    /// </summary>
    [TestClass]
    public class UnitTestOfRepository
    {
        public UnitTestOfRepository()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///获取或设置测试上下文，该上下文提供
        ///有关当前测试运行及其功能的信息。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 附加测试特性
        //
        // 编写测试时，可以使用以下附加特性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            LaughStarter.Run();
        }

        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            LaughStarter.Stop();
        }

        //
        // 在运行每个测试之前，使用 TestInitialize 来运行代码
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在每个测试运行完之后，使用 TestCleanup 来运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestRepositoryCRUD()
        {
            IRepository<Book> bookRepository = Resolver.GetService<IRepository<Book>>().UseDatabase("zzh1");

            //增
            Book b1 = new Book();
            b1.Name = "测试内容2";
            b1.Age = 29;
            bookRepository.Save(b1);

            Assert.AreNotEqual(b1.Id, 0);

            ////查
            //IEnumerable<Book> books = bookRepository.All;
            //Assert.AreNotEqual(books.Count(), 0);

            ////改
            //b1.Name = "修改后的测试内容1";
            //bookRepository.Update(b1);

            //Assert.AreEqual(b1.Name, "修改后的测试内容1");

            ////删
            //bookRepository.Delete(b1);
        }

        [TestMethod]
        public void 测试重复GET相同对象修改保存()
        {
            IRepository<Book> bookRepository1 = Resolver.GetService<IRepository<Book>>().UseDatabase("zzh1");
            //查
            Book b1 = bookRepository1.Fetch(i => i.Id == 1).FirstOrDefault();
            Book book = new Book();
            book.Id = b1.Id;
            book.Name = b1.Name;
            book.Age = b1.Age;

            //改
            IRepository<Book> bookRepository2 = Resolver.GetService<IRepository<Book>>().UseDatabase("zzh1");
            bookRepository2.Update(book);

            var b2 = bookRepository1.Get(1);

            Assert.AreEqual(b1.Name, "修改后的测试内容4");
        }
        [TestMethod]
        public void 测试重复GET相同对象修改保存2()
        {
            IRepository<Book> bookRepository2 = Resolver.GetService<IRepository<Book>>().UseDatabase("zzh1");
            Book b2 = bookRepository2.Get(i => i.Id == 1 && i.Age == 2009);
            bookRepository2.Update(b2);

            Assert.AreEqual(b2.Name, "修改后的测试内容4");
        }


        [TestMethod]
        public void TestSameRepositoryDifferentDatabases()
        {
            IRepository<Book> bookRepository = Resolver.GetService<IRepository<Book>>();

            bookRepository.UseDatabase("zzh1");
            Book b1 = new Book();
            b1.Name = "测试内容1";
            b1.Age = 2009;
            bookRepository.Save(b1);

            bookRepository.UseDatabase("zzh2");
            Book b2 = new Book();
            b2.Name = "测试内容2";
            b2.Age = 2012;
            bookRepository.Save(b2);
        }

        [TestMethod]
        public void TestDifferentRepositoryDifferentDatabases()
        {
            IRepository<Book> bookRepository1 = Resolver.GetService<IRepository<Book>>();
            bookRepository1.UseDatabase("zzh1");
            Book b1 = new Book();
            b1.Name = "测试内容1";
            b1.Age = 2009;
            bookRepository1.Save(b1);

            IRepository<Book> bookRepository2 = Resolver.GetService<IRepository<Book>>();
            bookRepository2.UseDatabase("zzh2");
            Book b2 = new Book();
            b2.Name = "测试内容2";
            b2.Age = 2012;
            bookRepository2.Save(b2);
        }

        [TestMethod]
        public void TestResolverServices()
        {
            ITest test = Resolver.GetService<ITest>();

            int c = test.Addition(4, 3);
        }

        [TestMethod]
        public void TestBuildSchema()
        {
            IEnumerable<IDatabase> databases = Resolver.GetService<IEnumerable<IDatabase>>();
            databases.Each(i => i.BuildSchema());
        }
    }
}
